VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsCrystalReport"
Attribute VB_GlobalNameSpace = True
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Private cmdReportHeader As New ADODB.Command
Private cmdReportDetail As New ADODB.Command
Private rsReportHeader As New ADODB.Recordset
Private rsReportDetail() As New ADODB.Recordset 'Dynamic Array
Private rptTransID As String
Private rspHeader As String
Private rspDetail As String
Private rspComp As String

Dim i As Integer
Dim cParam As String
Dim cReceiptName As String
Dim nSubRpt As Integer
Dim cSubRpt
Dim cSubSPRpt                           'rspDetail

Public RPTPath As String

Public Sub PrintReceipt(sModule As String, cTransID As String)
    On Error GoTo TheSource
    
    Dim rptPrint As CRAXDRT.Report
    Dim ReportApp As New CRAXDDRT.Application
    Dim cSubRptName As String
    Dim cSubRptCmp As String
        
    rptTransID = cTransID
    
    Dim pCN As New Connection
    pCN.CursorLocation = adUseClient
    pCN.ConnectionString = sConnString_
    pCN.Open
        
    Dim tmpRS As New Recordset
    tmpRS.Open "select cCode from System_Report where cCode = '" & sModule & "'", pCN, adOpenStatic, adLockReadOnly
    sModule = tmpRS!cCode
    tmpRS.Close
    
    Dim strSQL As String
    strSQL = "select lIsSP,nSubRptNo,cReportHeader,cReportDetail,cTranHeader,cTranDetail,cParam from sec_tran_report where cCode = '" & sModule & "' and cCompanyID = '" & sCompanyID_ & "'"
    tmpRS.Open strSQL, pCN, adOpenStatic, adLockReadOnly
    If tmpRS.RecordCount <> 0 Then
        cReceiptName = tmpRS!cRPTHeader
        cSubRptName = tmpRS!cRPTDetail
        rspHeader = tmpRS!cSPHeader
        rspDetail = tmpRS!cSPDetail
        cParam = tmpRS!cParameter

        cSubRpt = Split(cSubRptName, ",")
        cSubSPRpt = Split(rspDetail, ",")
        nSubRpt = UBound(cSubSPRpt)
    Else
        Exit Sub
    End If
    
     tmpRS.Close
        
    OpenReportRS cTransID, pCN
    
    Set rptPrint = ReportApp.OpenReport(RPTPath & "\Transaction\" & cReceiptName & ".rpt")
    DoEvents
    
    'For Dynamic Array SubReport
    If nSubRpt >= 1 Then
        For i = 0 To nSubRpt - 1
            rptPrint.OpenSubreport(cSubRpt(i)).Database.SetDataSource rsReportDetail(i)
        Next i
    End If
    rptPrint.Database.SetDataSource rsReportHeader
    
    frmReportViewer.Show
    Dim obj As New Application
    With frmReportViewer.CRViewer
        .ReportSource = rptPrint
        .PrintReport
        .ViewReport
        .DisplayGroupTree = False
        .Zoom (100)
    End With
             
    'ExportToPDF rptPrint
    
    'Audit trail
    Dim a As New clsDatabaseModules
    a.AuditLog sModule, cTransID, "Printed"
    Set a = Nothing

TheSource:
    If Err.Number = -2147206461 Then 'File not found

        MsgBox "Report file '" & cReceiptName & ".rpt' not found.", vbCritical, "ComUnion"
    ElseIf Err.Number = 3265 Then
        MsgBox Err.Description
        MsgBox "Stored Procedure '" & rspHeader & "' not found.", vbCritical, "ComUnion"
    End If

    Set rsReportHeader = Nothing
    
    For i = 0 To nSubRpt - 1
        Set rsReportDetail(i) = Nothing
    Next i
    
    Set cmdReportHeader = Nothing
    Set cmdReportDetail = Nothing
    Set rptPrint = Nothing
    Set ReportApp = Nothing

End Sub

Sub OpenReportRS(cTransID As String, pCN As Connection)
  
    rsReportHeader.CursorLocation = adUseClient
    With cmdReportHeader
        .ActiveConnection = pCN
        .CommandTimeout = 1000
        .CommandText = rspHeader
        .CommandType = adCmdStoredProc
        
        Dim tmpRS As New Recordset
        Dim a
        Dim strSQL As String
        strSQL = "select cType,cParamName,cModule,cDesc,cValue from System_Settings where cType = 'SQLReportSettings'"
        tmpRS.Open strSQL, pCN, adOpenStatic, adLockReadOnly
        a = Split(cParam, ",")
        Do Until tmpRS.EOF
            For i = 0 To UBound(a)
                If (tmpRS!cValue = a(i)) Then
                    Select Case tmpRS!cCode
                        Case "pCompID"
                            .Parameters(a(i)) = sCompanyID_
                        Case "pTranID"
                            .Parameters(a(i)) = cTransID
                    End Select
                End If
            Next i
            tmpRS.MoveNext
        Loop
        tmpRS.Close
        
    End With
    
    'For Sub Report Manipulation
    If nSubRpt >= 1 Then                                                                                           'if the report has a sub report
        ReDim rsReportDetail(0 To nSubRpt - 1) As New ADODB.Recordset   'Redim the dynamic array
        For i = 0 To nSubRpt - 1
            With cmdReportDetail
                .ActiveConnection = pCN
                .CommandTimeout = 1000
                .CommandText = cSubSPRpt(i) 'rspDetail
                .CommandType = adCmdStoredProc
                .Parameters("@cTranNo") = rptTransID
                .Parameters("@cCompID") = sCompanyID_
            End With
            rsReportDetail(i).CursorLocation = adUseClient
            Set rsReportDetail(i) = cmdReportDetail.Execute()
        Next i
    End If
       
    Set rsReportHeader = cmdReportHeader.Execute()

End Sub

